//
//  HWPromotionQrCodeViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/2/19.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWPromotionQrCodeViewController.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import "WXApi.h"

#import "HWerweiMaFenXiangView.h"


@interface HWPromotionQrCodeViewController (){
    
     SendMessageToQQReq *req;
    
    
}

@property (weak, nonatomic) IBOutlet UIView *beijingView;


@property (weak, nonatomic) IBOutlet UILabel *youxibiaotiLabel;
@property (weak, nonatomic) IBOutlet UIImageView *icoiamgeView;

@property (weak, nonatomic) IBOutlet UIImageView *erweiImageView;
@property (weak, nonatomic) IBOutlet UIButton *fuzhiButon;
@property (weak, nonatomic) IBOutlet UILabel *lianjiedizhiLabel;
@property (weak, nonatomic) IBOutlet UIView *lianjieView;
@property (strong,nonatomic) NSDictionary *dataDic;




@end

@implementation HWPromotionQrCodeViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 设置UI
    [self setupUI];
     
    
    // 发送网络请求
    [self sendNetworkRequest];
    
    
    

    
}

// 设置UI
- (void)setupUI {
    
    self.view.backgroundColor = beijingyanSe;
    self.navigationItem.title = @"推广二维码";
    self.lianjieView.backgroundColor = [UIColor whiteColor];
    
    
//    self.shanmianView.layer.cornerRadius = 12;
//    self.shanmianView.layer.masksToBounds = YES;
//    
//    self.lijieView.layer.cornerRadius = 12;
//      self.lijieView.layer.masksToBounds = YES;
    
    self.beijingView.layer.cornerRadius = 12;
    self.beijingView.layer.masksToBounds = YES;
    
    self.fuzhiButon.layer.cornerRadius = 12;
    self.fuzhiButon.layer.masksToBounds = YES;
    
    self.lianjieView.layer.cornerRadius = 12;
    self.lianjieView.layer.masksToBounds = YES;
    
    
    NSArray *fanlilistArr = @[@"微信",@"QQ",@"朋友圈"];
        NSArray   *paihangbangcoArr = @[@"weixing_ico",@"QQtupian_ico",@"pengyouquna_ico"];
        CGFloat hengjianjuLiin = (kScreenW-40 -  65*3)/4;
       
        for (int i = 0 ; i < fanlilistArr.count; i++) {
        
            int row = i/3;
            int line = i%3;
//            int shulian = (line*2+2) -1;
            UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(hengjianjuLiin+hengjianjuLiin*line+line*65, 400+row*47, 65,47 )];
            redview.tag = i;
            redview.backgroundColor = [UIColor whiteColor];
            [self.beijingView  addSubview:redview];
            
            
            UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imviewTaKaifu:)];
            [redview addGestureRecognizer:tap1];
            
            
            UIImageView *imview = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0,  24, 24)];
            imview.image = [UIImage imageNamed:paihangbangcoArr[i]];
            imview.userInteractionEnabled = YES;
            imview.tag = i;
            [redview addSubview:imview];
            
          //  [self.imageMuArr addObject:imview];
            
        
            
           // UILabel
            UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 65, 17)];
            nameLabel.text = fanlilistArr[i];
            nameLabel.textAlignment = NSTextAlignmentCenter;
            nameLabel.font = [UIFont systemFontOfSize:12];
            nameLabel.textColor = RGB(68, 68, 68);
            [redview addSubview:nameLabel];
         //   [self.labelMuArr addObject:nameLabel];
            
        
        }
        
    
    
    
    
    
    

    
}

// 发送网络请求
- (void)sendNetworkRequest {
    
    
    
     
         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         NSString *uid =  [HWLocallyStoredData getLoginUserID];
         NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
         NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
         NSDictionary *parametes = @{        @"ios" : @"1",
                                        @"ct" : @"app",
                                        @"ac" : @"qrcode",
                                        @"t" : tStr,
                                        @"uid":uid,
                                        @"sign" :signStrMd5
                                        };
        
        
            [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                  
                 

                  NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                  if ([codeStr intValue] == 1) {
                        NSLog(@"%@",response);
                      
                      
                      NSDictionary *dic = response[@"data"];
                      self.dataDic = dic;
                      
                      
                      
                      self.youxibiaotiLabel.text = [NSString stringWithFormat:@"%@",dic[@"title"]];
                      
                      
                      NSString *qre = dic[@"qrcode"];
                      [self.erweiImageView sd_setImageWithURL:[NSURL URLWithString:qre] placeholderImage:nil];
                      
                      self.lianjiedizhiLabel.text = [NSString stringWithFormat:@"%@",dic[@"url"]];
                      NSString *icon = dic[@"icon"];
                      [self.icoiamgeView sd_setImageWithURL:[NSURL URLWithString:icon] placeholderImage:nil];
                      
            
                  } else {
                      
                    [MBProgressHUD showError:response[@"message"]];
                                    
                
                      
                  }
                  
             
                  
              } failure:^(NSString *error) {
                 // [SVProgressHUD dismiss];
                  
                   [MBProgressHUD showSuccess:@"登录失败"];
                  [MBProgressHUD hideHUD];
                  
                  
              }];
        
    
    
    
    
    
    
    
}




// 复制Buton

- (IBAction)fuzhiButTap:(UIButton *)sender {
    
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.dataDic[@"url"];
    
    
    
    
    
    
    
}



- (void)imviewTaKaifu:(UITapGestureRecognizer *)sender {
    
    
    if ([HWGlobalConst isWechatAndQQ] == YES) {
        
    } else {
        HWerweiMaFenXiangView  *fenxiang = [[HWerweiMaFenXiangView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
        fenxiang.urlStr =self.dataDic[@"url"];
        [kKeyWindow addSubview:fenxiang];
        return ;
    }
    
    
    UIView *view = sender.view;
    
    if (view.tag == 0) {
        
        
         SDWebImageManager *manager =[SDWebImageManager sharedManager];
         //根据URLString去下载图片
          NSString *URLString = self.dataDic[@"icon"];
         __weak typeof(self) weakSelf = self;
           [manager loadImageWithURL:[NSURL URLWithString:URLString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
               
           } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
               
               if (finished) {
                   
                   NSLog(@" shuju %@",image);
                   
                   
                   
                         NSString *utf8String = self.dataDic[@"url"];
                         NSString *title = self.dataDic[@"title"];
                         NSString *description = [NSString stringWithFormat:@"%@",self.dataDic[@"title"]];
                         
                         //创建发送对象实例
                         SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
                         sendReq.bText = NO;//不使用文本信息
                         sendReq.scene = 0;
                         // 发送到聊天界面  0
                         // 发送到朋友圈    1
                         // 发送到微信收藏  2
                         //创建分享内容对象
                         WXMediaMessage *urlMessage = [WXMediaMessage message];
                         urlMessage.title = title;//分享标题 urlMessage.description = @"";
                         urlMessage.description= description;
                         
                         //分享描述
                         [urlMessage setThumbImage:image];
                         //分享图片,使用SDK的setThumbImage方法可压缩图片大小
                         //创建多媒体对象
                         WXWebpageObject *webObj = [WXWebpageObject object];
                         webObj.webpageUrl = utf8String;//分享链接
                       
                         //完成发送对象实例
                         urlMessage.mediaObject = webObj;
                         sendReq.message = urlMessage;
                         //发送分享信息
                         [WXApi sendReq:sendReq];
                   
                   
                   
                   
                   
                   
                   
               }
               
               
           }];
           
        
        
        
        
        
        
        NSLog(@"dldld");
        
    } else if ( view.tag == 1) {
        
        
        
               NSString *utf8String = self.dataDic[@"url"];
               NSString *title = self.dataDic[@"title"];
               NSString *description = [NSString stringWithFormat:@"%@",self.dataDic[@"title"]];
               
                NSString *previewImageUrl =self.dataDic[@"icon"];
                QQApiNewsObject *newsObj = [QQApiNewsObject
                                            objectWithURL:[NSURL URLWithString:utf8String]
                                            title:title
                                            description:description
                                            previewImageURL:[NSURL URLWithString:previewImageUrl]];
                 req = [SendMessageToQQReq reqWithContent:newsObj];
                 
                 QQApiSendResultCode sent = [QQApiInterface sendReq:req];
        
        
        
    } else if (view.tag == 2) {
        
        NSLog(@"1233");
        
        
        
         SDWebImageManager *manager =[SDWebImageManager sharedManager];
         //根据URLString去下载图片
          NSString *URLString = self.dataDic[@"icon"];
         __weak typeof(self) weakSelf = self;
           [manager loadImageWithURL:[NSURL URLWithString:URLString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
               
           } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
               
               if (finished) {
                   
                   NSLog(@" shuju %@",image);
                   
                   
                   
                         NSString *utf8String = self.dataDic[@"url"];
                         NSString *title = self.dataDic[@"title"];
                         NSString *description = [NSString stringWithFormat:@"%@",self.dataDic[@"title"]];
                         
                         //创建发送对象实例
                         SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
                         sendReq.bText = NO;//不使用文本信息
                         sendReq.scene = 1;
                         // 发送到聊天界面  0
                         // 发送到朋友圈    1
                         // 发送到微信收藏  2
                         //创建分享内容对象
                         WXMediaMessage *urlMessage = [WXMediaMessage message];
                         urlMessage.title = title;//分享标题 urlMessage.description = @"";
                         urlMessage.description= description;
                         
                         //分享描述
                         [urlMessage setThumbImage:image];
                         //分享图片,使用SDK的setThumbImage方法可压缩图片大小
                         //创建多媒体对象
                         WXWebpageObject *webObj = [WXWebpageObject object];
                         webObj.webpageUrl = utf8String;//分享链接
                       
                         //完成发送对象实例
                         urlMessage.mediaObject = webObj;
                         sendReq.message = urlMessage;
                         //发送分享信息
                         [WXApi sendReq:sendReq];
                   
                   
                   
                   
                   
                   
                   
               }
               
               
           }];
           
        
        
        
        
        
        
        
    }
    
    
    
}





@end
